package com.cyy.user.listener;

import com.aliyuncs.exceptions.ClientException;
import com.cyy.user.SmsApplication;
import com.cyy.user.util.SmsUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 消息监听器
 */
@Component
@RabbitListener(queues = "sms")
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;

    @Value("${aliyun.sms.template_code}")
    private String template_code;//模板编号

    @Value("${aliyun.sms.sign_name}")
    private String sign_name;//短信签名

    @RabbitHandler
    public void sendSms(Map<String,Object> message){
        System.out.println("验证码:" + message.get("code"));
        try {
            smsUtil.sendSms(message.get("mobile").toString(),template_code,sign_name," {\"code\":\""+ message.get("code") +"\"}");
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
